package com.ys.peony.ui.holder;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.interfaces.ItemClickListener;
import com.ys.peony.model.domain.ContractSortModel;

/**
 * Created by 59153 on 2018/3/27.
 */


public class ContractParentViewHolder extends BaseViewHolder {

    private Context mContext;
    private View view;

    private RelativeLayout rl_item;
    private TextView tv_name;
    private ImageView iv_arrow;
    private View top_area;
    Handler handler = new Handler(Looper.myLooper());

    public ContractParentViewHolder(Context context, View itemView) {
        super(itemView);
        this.mContext = context;
        this.view = itemView;
        rl_item = view.findViewById(R.id.rl_item);
        tv_name = view.findViewById(R.id.tv_name);
        iv_arrow = view.findViewById(R.id.iv_arrow);
        top_area = view.findViewById(R.id.top_area);
    }

    public void bindView(final ContractSortModel.FileTemplateBean model, final int pos, final ItemClickListener listener) {
        if (model == null) {
            return;
        }
        if (pos == 0) {
            top_area.setVisibility(View.VISIBLE);
        } else {
            top_area.setVisibility(View.GONE);
        }
        if (model.isExpand()) {
            iv_arrow.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.contract_down));
        } else {
            iv_arrow.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.contract_up));
        }
        tv_name.setText(model.getName());
        rl_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    if (model.isExpand()) {
                        model.setExpand(false);
                        listener.onHideChildren(pos);
                        rotationExpandIcon(0, -180, 0);


                    } else {
                        model.setExpand(true);
                        listener.onExpandChildren(pos);
                        rotationExpandIcon(0, 180, 1);
                    }
                }
            }
        });
    }

    private void rotationExpandIcon(final float from, float to, final int i) {
        /*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ObjectAnimator animator = ObjectAnimator.ofFloat(iv_arrow, "rotation", from, to);
            animator.setDuration(500);
            animator.start();
        }*/
        Animation animation = new RotateAnimation(from, to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);
        //animation.setFillAfter(true);//设置为true，动画转化结束后被应用
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animation.cancel();
                if (i == 0) {
                    iv_arrow.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.contract_up));
                } else {
                    iv_arrow.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.contract_down));
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        iv_arrow.startAnimation(animation);//开始动画
    }
}
